package payroll.webapp

class EmpSalaryDefinitionController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [empSalaryDefinitionInstanceList: EmpSalaryDefinition.list(params), empSalaryDefinitionInstanceTotal: EmpSalaryDefinition.count()]
    }

    def create = {
        /*def empSalaryDefinitionInstance = new EmpSalaryDefinition()
        empSalaryDefinitionInstance.properties = params
        return [empSalaryDefinitionInstance: empSalaryDefinitionInstance]*/
		if (request.method == 'GET') {
			def empsaldef = new EmpSalaryDefinition()
			//def salcalcTypes = new SalaryCalcTypes()
			empsaldef.properties = params		
			[empsaldef		 : empsaldef,
			 salCalcTypes	 : SalaryCalcTypes.list()]
		}
		else {
			def empsaldef = new EmpSalaryDefinition(params['empsaldef'])
			if (empsaldef.save()) {
				redirect action:'list'
			}
			else {
				render 	view	:  'add', 
						model	:  	[empsaldef		 : empsaldef,
							 		 salCalcTypes	 : SalaryCalcTypes.list()]
			}
		}
    }

    def save = {
        def empSalaryDefinitionInstance = new EmpSalaryDefinition(params)
        if (empSalaryDefinitionInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'empSalaryDefinition.label', default: 'EmpSalaryDefinition'), empSalaryDefinitionInstance.id])}"
            redirect(action: "show", id: empSalaryDefinitionInstance.id)
        }
        else {
            render(view: "create", model: [empSalaryDefinitionInstance: empSalaryDefinitionInstance])
        }
    }

    def show = {
        def empSalaryDefinitionInstance = EmpSalaryDefinition.get(params.id)
        if (!empSalaryDefinitionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'empSalaryDefinition.label', default: 'EmpSalaryDefinition'), params.id])}"
            redirect(action: "list")
        }
        else {
            [empSalaryDefinitionInstance: empSalaryDefinitionInstance]
        }
    }

    def edit = {
        def empSalaryDefinitionInstance = EmpSalaryDefinition.get(params.id)
        if (!empSalaryDefinitionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'empSalaryDefinition.label', default: 'EmpSalaryDefinition'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [empSalaryDefinitionInstance: empSalaryDefinitionInstance]
        }
    }

    def update = {
        def empSalaryDefinitionInstance = EmpSalaryDefinition.get(params.id)
        if (empSalaryDefinitionInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (empSalaryDefinitionInstance.version > version) {
                    
                    empSalaryDefinitionInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'empSalaryDefinition.label', default: 'EmpSalaryDefinition')] as Object[], "Another user has updated this EmpSalaryDefinition while you were editing")
                    render(view: "edit", model: [empSalaryDefinitionInstance: empSalaryDefinitionInstance])
                    return
                }
            }
            empSalaryDefinitionInstance.properties = params
            if (!empSalaryDefinitionInstance.hasErrors() && empSalaryDefinitionInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'empSalaryDefinition.label', default: 'EmpSalaryDefinition'), empSalaryDefinitionInstance.id])}"
                redirect(action: "show", id: empSalaryDefinitionInstance.id)
            }
            else {
                render(view: "edit", model: [empSalaryDefinitionInstance: empSalaryDefinitionInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'empSalaryDefinition.label', default: 'EmpSalaryDefinition'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def empSalaryDefinitionInstance = EmpSalaryDefinition.get(params.id)
        if (empSalaryDefinitionInstance) {
            try {
                empSalaryDefinitionInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'empSalaryDefinition.label', default: 'EmpSalaryDefinition'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'empSalaryDefinition.label', default: 'EmpSalaryDefinition'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'empSalaryDefinition.label', default: 'EmpSalaryDefinition'), params.id])}"
            redirect(action: "list")
        }
    }
}
